package cache

import (
	storage "ozonTest/internal/dal/cache"
	"ozonTest/internal/domain/comments/model"
	model2 "ozonTest/internal/domain/posts/model"
	"testing"
	"time"
)

func TestCreatePost(t *testing.T) {
	db := &storage.Database{
		Comments: make(map[int]*model.Comment),
		Posts:    make(map[int]*model2.Post),
	}

	repo := NewPostsRepo(db)

	post := &model2.Post{
		Title:           "Test Post",
		Content:         "This is a test post.",
		CommentsEnabled: true,
	}

	err := repo.CreatePost(post)
	if err != nil {
		t.Errorf("Error creating post: %v", err)
	}

	if post.ID != 1 {
		t.Errorf("Expected post ID to be 1, got %d", post.ID)
	}

	if len(db.Posts) != 1 {
		t.Errorf("Expected 1 post in the database, got %d", len(db.Posts))
	}

	if db.Posts[1] != post {
		t.Errorf("Expected post in database to be %+v, got %+v", post, db.Posts[1])
	}
}

func TestGetPost(t *testing.T) {
	db := &storage.Database{
		Comments: make(map[int]*model.Comment),
		Posts:    make(map[int]*model2.Post),
	}

	repo := NewPostsRepo(db)

	post := &model2.Post{
		ID:              1,
		Title:           "Test Post",
		Content:         "This is a test post.",
		CommentsEnabled: true,
		CreatedAt:       time.Now(),
	}
	db.Posts[1] = post

	retrievedPost, err := repo.GetPost(1)
	if err != nil {
		t.Errorf("Error getting post: %v", err)
	}

	if retrievedPost != post {
		t.Errorf("Expected retrieved post to be %+v, got %+v", post, retrievedPost)
	}

	_, err = repo.GetPost(2)
	if err == nil || err.Error() != "post not found" {
		t.Errorf("Expected error 'post not found', got %v", err)
	}
}

func TestGetAllPosts(t *testing.T) {
	db := &storage.Database{
		Comments: make(map[int]*model.Comment),
		Posts:    make(map[int]*model2.Post),
	}

	repo := NewPostsRepo(db)

	post1 := &model2.Post{
		ID:              1,
		Title:           "Test Post 1",
		Content:         "This is test post 1.",
		CommentsEnabled: true,
		CreatedAt:       time.Now(),
	}
	post2 := &model2.Post{
		ID:              2,
		Title:           "Test Post 2",
		Content:         "This is test post 2.",
		CommentsEnabled: true,
		CreatedAt:       time.Now(),
	}
	db.Posts[1] = post1
	db.Posts[2] = post2

	posts, err := repo.GetAllPosts()
	if err != nil {
		t.Errorf("Error getting all posts: %v", err)
	}

	if len(posts) != 2 {
		t.Errorf("Expected 2 posts, got %d", len(posts))
	}

	if posts[0] != *post1 && posts[1] != *post2 {
		t.Errorf("Expected posts to be %+v and %+v, got %+v", *post1, *post2, posts)
	}
}

func TestDeletePost(t *testing.T) {
	db := &storage.Database{
		Comments: make(map[int]*model.Comment),
		Posts:    make(map[int]*model2.Post),
	}

	repo := NewPostsRepo(db)

	post := &model2.Post{
		ID:              1,
		Title:           "Test Post",
		Content:         "This is a test post.",
		CommentsEnabled: true,
		CreatedAt:       time.Now(),
	}
	db.Posts[1] = post

	err := repo.DeletePost(1)
	if err != nil {
		t.Errorf("Error deleting post: %v", err)
	}

	if len(db.Posts) != 0 {
		t.Errorf("Expected 0 posts in the database, got %d", len(db.Posts))
	}

	err = repo.DeletePost(2)
	if err == nil || err.Error() != "post not found" {
		t.Errorf("Expected error 'post not found', got %v", err)
	}
}

func TestChangeCommentsAllowed(t *testing.T) {
	db := &storage.Database{
		Comments: make(map[int]*model.Comment),
		Posts:    make(map[int]*model2.Post),
	}

	repo := NewPostsRepo(db)

	post := &model2.Post{
		ID:              1,
		Title:           "Test Post",
		Content:         "This is a test post.",
		CommentsEnabled: true,
		CreatedAt:       time.Now(),
	}
	db.Posts[1] = post

	err := repo.ChangeCommentsAllowed(1, false)
	if err != nil {
		t.Errorf("Error changing comments allowed: %v", err)
	}

	if db.Posts[1].CommentsEnabled != false {
		t.Errorf("Expected comments enabled to be false, got %v", db.Posts[1].CommentsEnabled)
	}

	err = repo.ChangeCommentsAllowed(2, true)
	if err == nil || err.Error() != "post not found" {
		t.Errorf("Expected error 'post not found', got %v", err)
	}
}
